﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Popup.Master" AutoEventWireup="true"
    CodeBehind="CustomerPropertyDetail.aspx.cs" Inherits="PromotionEngine.CustomerPropertyDetail" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <script type="text/javascript">
        var oCustomerPropertyDetail = {};
        var isInsert = false;
        var gird;
        $(document).ready(function () {
            var propertyForDetailID = $('#<%=lblPropertyID.ClientID%>').val();
            gird = $("#gridDetail").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            type: "GET",
                            url: "CustomerPropertyDetail.aspx/GetCustomerPropertyDetails",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json"
                        },
                        destroy: {
                            type: "GET",
                            url: "CustomerPropertyDetail.aspx/DeleteCustomerPropertyDetail",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json"
                        },
                        parameterMap: function (data, operation) {
                            if (operation === "read") {
                                return { propertyID: propertyForDetailID };
                            }
                            if (operation === "destroy") {
                                return { propertyDetailID: data.PropertyDetailID };
                            }
                        },
                        error: function (a) {

                        },
                        serverPaging: true,
                        serverSorting: true,
                        serverFiltering: true
                    },
                    type: "odata", //Important!!!!
                    schema: {
                        data: function (a) {
                            if (a.d != null) {
                                var data = a.d.Data;
                                return data;
                            }
                        },
                        total: "d.Count",
                        model: {
                            id: "PropertyDetailID",
                            fields: {
                                PropertyID: { type: "number" },
                                PropertyDetailID: { type: "string" },
                                PropertyDetailName: { type: "string", validation: { required: true} },
                                PropertyDetailCode: { type: "string", validation: { required: true} }
                            }
                        }
                    }
                },

                sortable: true,
                pageable: {
                    pageSize: 20,
                    refresh: true
                },
                filterable: true,
                reorderable: true,
                resizable: true,
                height: 450,
                toolbar: [{ name: 'create', text: 'Add New Property Detail' }, { name: 'excel'}],
                excel: {
                    fileName: "Customer Property Detail List.xlsx",
                    filterable: true
                },
                batch: true,
                columns: [
                            { field: "PropertyID", title: "Property ID", width: 300, hidden: true },
                            { field: "PropertyDetailID", title: "Property ID", width: 300, hidden: true },
                            { field: "PropertyDetailCode", title: "Detail Code", width: 300 },
                            { field: "PropertyDetailName", title: "Detail Name" },
                                 { command: ["edit", "destroy"], title: "Action", width: "200px" }
                            ],
                editable: {
                    mode: "inline",
                    confirmation: "Are you sure you want to remove this property detail?"
                },
                edit: function (e) {
                    if (e.model.isNew()) {
                        isInsert = true;
                        $("a.k-grid-update")[0].innerHTML = "<span class='k-icon k-update'></span>Save";
                    }
                    else {
                        isInsert = false;
                    }
                },
                save: function (e) {
                    var listTextBox = $("input:text");
                    for (var i = 0; i < listTextBox.length; i++) {
                        var name = listTextBox[i].name;
                        if (name.length > 0) {
                            oCustomerPropertyDetail[name] = $("[name=" + name + "]").val();
                        }
                    }
                    oCustomerPropertyDetail.PropertyID = propertyForDetailID;
                    //Update or Insert Company
                    $.ajax({
                        type: "POST",
                        url: "CustomerPropertyDetail.aspx/UpdateCustomerPropertyDetail",
                        data: JSON.stringify({
                            oCustomerPropertyDetail: JSON.stringify(oCustomerPropertyDetail),
                            isInsert: isInsert
                        }),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            //Reload Data 
                            if (isInsert) {
                                gird.dataSource.read();
                            }
                            isInsert = false;
                        },
                        error: function (xhr, status, err) {
                            var err = eval("(" + xhr.responseText + ")");
                            alert(err.Message);
                        }
                    });
                    this.refresh();
                }
            }).data("kendoGrid");
        });
    </script>
    <h2>
        Property Code:&nbsp;
        <asp:Label ID="lblPropertyCode" runat="server"></asp:Label>
        - Property Name:
        <asp:Label ID="lblPropertyName" runat="server"></asp:Label>
        <asp:HiddenField ID="lblPropertyID" runat="server"></asp:HiddenField>
    </h2>
    <hr/>
    <div id="gridDetail">
    </div>
</asp:Content>
